package top.canyue.word_range_service.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import top.canyue.word_range.annotation.NotNullPassword;
import top.canyue.word_range.annotation.ValidatePhoneNumber;
import top.canyue.word_range.pojo.dto.LoginDto;
import top.canyue.word_range.pojo.dto.PageDTO;
import top.canyue.word_range.pojo.dto.UserDto;
import top.canyue.word_range.pojo.query.UserQuery;
import top.canyue.word_range.pojo.vo.LoginResponseVo;
import top.canyue.word_range.pojo.vo.UserVo;
import top.canyue.word_range.result.Result;
import top.canyue.word_range_service.service.UserLoginService;
import top.canyue.word_range_service.service.UserService;

/**
 * 用户相关接口
 */
@RestController
@RequestMapping("/apiV1/user")
@RequiredArgsConstructor
public class UserController {
    final UserService userService;
    final UserLoginService userLoginService;

    /**
     * 用户登录
     */
    @NotNullPassword
    @PostMapping("/login")
    public Result<LoginResponseVo> login(@RequestBody LoginDto loginDto) {
        return userLoginService.login(loginDto);
    }

    /**
     * 根据JWT，从缓存获取已经登录用户信息
     */
    @GetMapping("/info/byJwt")
    public Result<UserVo> getUserByToken(@RequestHeader String JWT){
        return userLoginService.getUserByToken(JWT);
    }

    /**
     * 更新当前用户信息
     */
    @PutMapping("/info/ByJwt")
    public Result<UserVo> updateUserByJwt(@RequestHeader String JWT,@RequestBody UserDto userDto) {
        return userService.updateUserByJwt(JWT,userDto);
    }

    /**
     * 对当前登录用户进行销户操作
     */
    @DeleteMapping("/info/ByJwt")
    public Result<Object> deleteUserByJwt(){
        return userService.deleteUserByJwt();
    }

    /**
     * 返回当前用户的积分
     */
    @GetMapping("/activityIntegral/ByJwt")
    public Result<Integer> getActivityIntegral(@RequestHeader String JWT) {
        return userService.getActivityIntegral();
    }

    /**
     * 用户注册
     */
    @PostMapping("/logon")
    @NotNullPassword
    @ValidatePhoneNumber
    public Result<Object> logon(@RequestBody UserDto userDto) {
        return userLoginService.logon(userDto);
    }

    /**
     * 用户注销(销毁Token)
     * @param JWT 登录时下发的token
     */
    @PostMapping("/logout")
    public Result<Object> logout(@RequestHeader String JWT) {
        return userLoginService.logout();
    }

    /**
     * 用户分页复杂查询
     */
    @GetMapping("/page")
    public PageDTO<UserVo> getUsersPage(@RequestHeader String JWT, UserQuery userQuery) {
        return userService.getUsersPage(userQuery);
    }

    /**
     * 修改自己的密码
     * <p>通过JWT控制修改的人，非管理员可调用，只能改自己的密码</p>
     * <p>目前密码的修改操作，将在用户下次登录时生效</p>
     */
    @PostMapping("/resetPassword")
    public Result<Object> resetPassword(@RequestHeader String JWT,@RequestParam String newPassword) {
        return userService.resetPassword(JWT,newPassword);
    }

    /**
     * 获取当前用户注册天数
     */
    @GetMapping("/registerDays")
    public Result<Long> getRegisterDays(@RequestHeader String JWT) {
        return userService.getRegisterDays(JWT);
    }

    /**
     * 基于ID拿用户信息
     */
    @GetMapping("/{id}")
    public Result<UserVo> getUserById(@PathVariable String id, @RequestHeader String JWT) {
        return userService.getUserById(id);
    }

    /**
     * 基于用户ID修改用户信息
     */
    @PutMapping("/{id}")
    @ValidatePhoneNumber
    public Result<UserVo> updateUserById(@RequestHeader String JWT, @PathVariable String id, @RequestBody UserDto userDto) {
        return userService.updateUserById(id,userDto);
    }

    /**
     * 基于用户ID删除用户
     */
    @DeleteMapping("/{id}")
    public Result<Object> deleteUserById(@RequestHeader String JWT, @PathVariable String id) {
        return userService.deleteUserById(id);
    }

}
